package com.example.service.impl;

import com.example.dto.StoreDTO;
import com.example.dto.StorePageQueryDTO;
import com.example.entity.Store;
import com.example.entity.User;
import com.example.mapper.StoreDao;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.StoreService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Store)表服务实现类
 *
 * @author makejava
 * @since 2024-03-21 17:27:37
 */
@Service("storeService")
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreDao storeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Store queryById(Long id) {
        return this.storeDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param store 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Store> queryByPage(Store store, PageRequest pageRequest) {
        long total = this.storeDao.count(store);
        return new PageImpl<>(this.storeDao.queryAllByLimit(store, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param store 实例对象
     * @return 实例对象
     */
    @Override
    public Store insert(Store store) {
        this.storeDao.insert(store);
        return store;
    }

    @Override
    public Store insert(StoreDTO storeDTO) {
        Store store = new Store();
        BeanUtils.copyProperties(storeDTO,store);
        this.storeDao.insert(store);
        return store;
    }

    /**
     * 修改数据
     *
     * @param store 实例对象
     * @return 实例对象
     */
    @Override
    public Store update(Store store) {
        this.storeDao.update(store);
        return this.queryById(store.getId());
    }

    @Override
    public Store update(StoreDTO storeDTO) {
        Store store = new Store();
        store = storeDao.getByName(storeDTO.getName());
        if(store==null || store.getId() == storeDTO.getId()){
            if(store == null)
                store = new Store();
            BeanUtils.copyProperties(storeDTO,store);
            this.storeDao.update(store);
            return this.queryById(store.getId());
        }
        else return store;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.storeDao.deleteById(id) > 0;
    }

    @Override
    public PageResult page(StorePageQueryDTO storePageQueryDTO) {
        PageHelper.startPage(storePageQueryDTO.getPage(),storePageQueryDTO.getPageSize());
        com.github.pagehelper.Page<Store> page = storeDao.pageQuery(storePageQueryDTO);
        int pageSize = (int) page.getTotal();
        List<Store> result = page.getResult();
        return new PageResult(pageSize,result);
    }
}
